//
//  PAHeadPicViewController.m
//  PAMobileWallet
//
//  Created by Pay_SMP002 on 13-7-3.
//  Copyright (c) 2013年 平安付. All rights reserved.
//

#import "PAHeadPicViewController.h"
#import "PAHeadPicView.h"
#import "PublicDefine.h"
#import "PANavView.h"
#import "PARealNameAuthViewController.h"
#import "PAPersonalViewController.h"
#import "PAPersonalCenterView.h"

#import "NSData+Extend.h"
#import "ASIFormDataRequest.h"

#import "PADataCenter.h"
#import "PANetworkManager.h"

#import <QuartzCore/QuartzCore.h>

@interface PAHeadPicViewController ()

@end

@implementation PAHeadPicViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self)
    {
        headPicView = [[PAHeadPicView alloc] initWithFrame:RECT_PHONE];
        headPicView.parentController = self;
        headPicView.backgroundColor = BGVIEW_COLOR;
        self.view = headPicView;
        
        navView = [[PANavView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 44)];
        navView.delegate = self;
        [navView setTitle:@"设置头像"];
        [headPicView addSubview:navView];
    }
    return self;
}

#pragma mark - PANavViewDelegate methods

- (void)sendSelf:(PANavView *)sendView
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)setMode:(HEADPICMODE)mode
{
    [headPicView setShowMode:mode];
}

- (void)testNext:(id)sender
{
    UIButton * receivebButton = (UIButton *)sender;
    if (receivebButton.tag == 1)
    {
        [self getphoto:receivebButton getType:HEADPICMODE_register];
    }
    
    if (receivebButton.tag == 0)
    {
        imageData = UIImageJPEGRepresentation(self.photoImage, 0.5);//UIImagePNGRepresentation(self.photoImage);
        NSString *base64String = [imageData base64Encoding];
        
        NSMutableDictionary * postingDic = [[NSMutableDictionary alloc] initWithDictionary:@{k_operationType:@"op_register_head_image",k_loginId:userPhoneNum,k_imageType:[NSNumber numberWithInt:1],k_headImage:base64String}];
        [[PANetworkManager sharedInstance] reqAsynParamDic:postingDic userInfo:nil delegate:self];
    }
}

- (void)viewDidLoad
{
    [super viewDidLoad];
}

- (void)setImage:(UINavigationBar *)getNavBar
{
    float version = [[[UIDevice currentDevice] systemVersion] floatValue];
    UIImage *backgroundImage = [UIImage imageNamed:@"注册-顶部.png"];
    if (version >= 5.0)
    {
        [self.navigationController.navigationBar setBackgroundImage:backgroundImage forBarMetrics:UIBarMetricsDefault];
    }
    else
    {
        [self.navigationController.navigationBar insertSubview:[[UIImageView alloc] initWithImage:backgroundImage] atIndex:1];
    }
}

- (void)getphoto:(UIButton *)sender getType:(HEADPICMODE)headType
{
    UIActionSheet *choosePhotoActionSheet;
    
    if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
    {
        choosePhotoActionSheet = [[UIActionSheet alloc] initWithTitle:NSLocalizedString(@"请选择上传方式", @"")
                                                             delegate:self
                                                    cancelButtonTitle:NSLocalizedString(@"cancel", @"")
                                               destructiveButtonTitle:nil
                                                    otherButtonTitles:NSLocalizedString(@"拍照选择", @""),NSLocalizedString(@"从相册选择", @""), nil];
    }
    else
    {
        choosePhotoActionSheet = [[UIActionSheet alloc] initWithTitle:NSLocalizedString(@"choose_photo", @"")
                                                             delegate:self
                                                    cancelButtonTitle:NSLocalizedString(@"cancel", @"")
                                               destructiveButtonTitle:nil
                                                    otherButtonTitles:NSLocalizedString(@"拍照选择", @""),NSLocalizedString(@"从相册选择", @""), nil];
    }
    
    [choosePhotoActionSheet showInView:self.view];
}

#pragma mark - UIActionSheetDelegate

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
	NSUInteger sourceType = 0;
    if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
    {
        switch (buttonIndex)
        {
            case 0:
                sourceType = UIImagePickerControllerSourceTypeCamera;
                break;
            case 1:
                sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
                break;
            case 2:
                return;
        }
    }
    else
    {
        if (buttonIndex == 2)
        {
            return;
        }
        else
        {
            sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
        }
    }
    
	UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
	imagePickerController.delegate = self;
	imagePickerController.allowsEditing = YES;
    imagePickerController.sourceType = sourceType;
	//[self presentModalViewController:imagePickerController animated:YES];
    [self presentViewController:imagePickerController animated:YES completion:nil];
}

- (void)sendPhoneNum:(NSString *)getPhoneNum
{
    userPhoneNum = getPhoneNum;
}

#pragma mark - UIImagePickerControllerDelegate

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    [picker dismissViewControllerAnimated:YES completion:nil];
	self.photoImage = [info objectForKey:UIImagePickerControllerEditedImage];
    
    [headPicView.photoImageView setHeadImage:self.photoImage];
    headPicView.photoImageView.layer.masksToBounds=YES;
    headPicView.photoImageView.layer.cornerRadius=211.0/2;//最重要的是这个地方要设成imgview高的一半

    if (self.photoImage)
    {
        headPicView.mobileButton.enabled = YES;
        [headPicView.mobileButton setBackgroundImage:[UIImage imageNamed:@"button_A1.png"] forState:UIControlStateNormal];
        [headPicView.mobileButton setBackgroundImage:[UIImage imageNamed:@"button_A2.png"] forState:UIControlStateHighlighted];
        headPicView.mobileButton.titleLabel.textColor = ABLE_COLOR;
    }
}

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)paRequestRespons:(NSDictionary *)responsDic userInfo:(NSDictionary *)userInfo
{
    NSLog(@"responDic:%@",responsDic);
    
    NSString *op = [responsDic objectForKey:k_operationType];
    if ([op isEqualToString:@"op_register_head_image"])
    {
        if (!realAuthController)
        {
            realAuthController = [[PARealNameAuthViewController alloc] initWithNibName:nil bundle:nil];
            realAuthController.parentController = self;
        }
        markType = 2;
        [self.navigationController pushViewController:realAuthController animated:YES];
    }
}

- (void)sendmarkType:(int)getmarkType
{
    markType = getmarkType;
}

- (void)viewWillAppear:(BOOL)animated
{
    [self.navigationController setNavigationBarHidden:YES animated:NO];
    
    if (markType == 1 )
    {
        headPicView.mobileButton.enabled = NO;
        [headPicView.mobileButton setBackgroundImage:[UIImage imageNamed:@"button_B1.png.png"] forState:UIControlStateNormal];
        headPicView.mobileButton.titleLabel.textColor = ENABLE_COLOR;
    }
    if (markType == 2)
    {
        headPicView.mobileButton.enabled = YES;
        [headPicView.mobileButton setBackgroundImage:[UIImage imageNamed:@"button_A1.png"] forState:UIControlStateNormal];
        [headPicView.mobileButton setBackgroundImage:[UIImage imageNamed:@"button_A2.png"] forState:UIControlStateHighlighted];
        [headPicView.mobileButton setTitleColor:ABLE_COLOR forState:UIControlStateNormal];
        [headPicView.mobileButton setTitleColor:ABLE_COLOR forState:UIControlStateHighlighted];
    }
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

@end
